import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sn
import datetime
from IPython.display import display, HTML

#set params
params = {'legend.fontsize': 'x-large',
          'figure.figsize': (30,10),
          'axes.labelsize': 'x-large',
          'axes.titlesize': 'x-large',
          'xtick.labelsize': 'x-large',
          'ytick.labelsize': 'x-large'}
sn.set_style('whitegrid')
sn.set_context('talk')

plt.rcParams.update(params)
pd.options.display.max_colwidth = 600

#pandas display data frames as tables


#读取数据
train = pd.read_csv("D:/AITest/pro_python/bikeshare_data/day.csv")
# print(train.head())
#打印数据维度：（731， 16）
# print("train:" + str(train.shape))
# print(train.info())
# print(train.describe())

#类别性特征分布
categorical_features = ['season', 'mnth', 'weathersit', 'weekday']
# for col in categorical_features:
#     print('\n%s属性的不同取值和出现次数'%col)
#     print(train[col].value_counts())
#     train[col] = train[col].astype('object')

#数字型特征分布
numerical_features = ['temp', 'atemp', 'hum', 'windspeed']
# train[numerical_features].hist()
#
# #特征和目标之间的关系
# sn.violinplot(data=train[['yr', 'cnt']], x="yr", y="cnt")
#
#
# import datetime
#
# train['date'] = pd.to_datetime(train['dteday'])
# train['dayofyear'] = train['date'].dt.dayofyear#减今年的第几天
#
# fig, ax = plt.subplots()
# sn.pointplot(data=train[['dayofyear', 'cnt', 'yr']], x="dayofyear", y="cnt", hue='yr', ax=ax)
# ax.set(title='dayly distribution of counts')
# plt.show()

#季节与骑行量的关系
# sn.violinplot(data=train[['season', 'cnt']], x="season", y="cnt")
#
# fig, ax = plt.subplots()
# sn.barplot(data=train[['season', 'cnt']], x="season", y="cnt")
# ax.set(title="Season distribution of counts")
# plt.show()

#月份与骑行量的关系
# fig, ax = plt.subplots()
# sn.barplot(data=train[['mnth', 'cnt']], x="mnth", y="cnt")
# ax.set(title="Month distribution of counts")
# plt.show()
#
#天气与骑行量的关系
# fig, ax = plt.subplots()
# sn.barplot(data=train[['weathersit', 'cnt']], x="weathersit", y="cnt")
# ax.set(title="weathersit distribution of counts")
# plt.show()
#
# fig, (ax1, ax2) = plt.subplots(ncols=2)
# sn.barplot(data=train, x='holiday', y='cnt', ax=ax1)
# sn.barplot(data=train,x='workingday', y='cnt', ax=ax2)
# plt.show()

# corrMatt = train[["temp", "atemp", "hum", "windspeed", "casual", "registered", "cnt"]].corr()
# mask = np.array(corrMatt)
# mask[np.tril_indices_from(mask)] = False
# sn.heatmap(corrMatt, mask=mask, vmax=.8, square=True, annot=True)
#
#对类别型特征，观察其取值范围及直方图
categorical_features = ['season', 'mnth', 'weathersit', 'weekday']
#数据类型转换成object，才能被get_dummies处理
for col in categorical_features:
    train[col] = train[col].astype('object')

X_train_cat = train[categorical_features]
X_train_cat = pd.get_dummies(X_train_cat)
X_train_cat.head()

#对数字型特征进行标准化
from sklearn.preprocessing import MinMaxScaler
mn_X = MinMaxScaler()
log_cnt = np.log1p(train['cnt'])
numerical_features = ['temp', 'atemp', 'hum', 'windspeed']

temp = mn_X.fit_transform(train[numerical_features])

X_train_num = pd.DataFrame(data=temp, columns=numerical_features, index=train.index)
X_train_num.head()

#Join categorical and numerical features
X_train = pd.concat([X_train_cat, X_train_num, train['holiday'], train['workingday']], axis=1, ignore_index=False)
X_train.head()

FE_train = pd.concat([train['instant'], X_train, train['yr'], train['cnt']], axis=1)
FE_train.to_csv('FE_day.csv', index=False)
FE_train.head()
print(FE_train.info())